using System;
using System.Web.Caching;
using System.Web;
using Portal.User.Security;
using Portal.BO.Editoral.Newslist;
using Portal.BO.Editoral.PhotoAlbum;
using System.Data;
using Portal.BO.Editoral.CommentManagement;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
namespace Portal.GUI.EditoralOffice.MainOffce.Menu
{
    public partial class MnuBtv : System.Web.UI.UserControl
    {
        #region private method
        private void CreateMenuNews()
        {
            DataTable table = CommonHelper.GetDataPermission(RolePrefix.BIENTAPVIEN, 1);
            DataView dv = table.DefaultView;
            dv.Sort = "Permission_ID";
            //dlMenuNews.DataSource = dv;
            //dlMenuNews.DataBind();
            dtlMenuNews.DataSource = dv;
            dtlMenuNews.DataBind();
            if (table.Rows.Count == 0)
            {
                table1.Visible = false;
            }
        }
        private void CreateMenuAlbum()
        {
            DataTable table = CommonHelper.GetDataPermission(RolePrefix.BIENTAPVIEN, 2);
            DataView dv = table.DefaultView;
            dv.Sort = "Permission_ID";
            //dlMenuAlbum.DataSource = dv;
            //dlMenuAlbum.DataBind();
            dtlMenuAlbum.DataSource = dv;
            dtlMenuAlbum.DataBind();
            if (table.Rows.Count == 0)
            {
                table2.Visible = false;
            }
        }
        #endregion
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
            #region Code Cu
            string strCacheName = "IsTBTOrPTK" + Page.User.Identity.Name;
            if (HttpContext.Current.Cache[strCacheName] == null)
            {
                MainSecurity objSecu = new MainSecurity();
                Role objPermis = objSecu.GetRole(Page.User.Identity.Name, Portal.API.Config.CurrentChannel);
                if (objPermis.isTongBienTap || objPermis.isPhuTrachKenh)
                {
                    SaveCache(strCacheName, "false");
                    Div1.Visible = false;
                }
                else
                {
                    SaveCache(strCacheName, "true");
                }
            }
            else
            {
                bool isOk = Convert.ToBoolean(HttpContext.Current.Cache[strCacheName].ToString());
                Div1.Visible = isOk;
            }
            #endregion
            #region Code Cu
            /*

            itemAddNews.NavigateUrl = "~/office/add_btv.chn";
            itemListNews.NavigateUrl = "~/office/templist_btv.chn";
            itemDelList.NavigateUrl = "~/office/dellist_btv.chn";
            itemEditWait.NavigateUrl = "~/office/editwaitlist_btv.chn";
            itemSendEditWait.NavigateUrl = "~/office/sendapprovallist.chn";
            itemEditedList.NavigateUrl = "~/office/editinglist_btv.chn";
            itemBackList.NavigateUrl = "~/office/backlist_btv.chn";
            itemMedia.NavigateUrl = "~/office/mediamngr.chn";

            albumAdd.NavigateUrl = "~/office/album_add_btv.chn";
            albumList.NavigateUrl = "~/office/album_templist_btv.chn";
            albumDelList.NavigateUrl = "~/office/album_dellist_btv.chn";
            albumEditWait.NavigateUrl = "~/office/album_editwaitlist_btv.chn";
            albumSendEditWait.NavigateUrl = "~/office/album_sendapprovallist.chn";
            albumEditedList.NavigateUrl = "~/office/album_editinglist_btv.chn";
            albumBackList.NavigateUrl = "~/office/album_backlist_btv.chn";

            ltrListNews.Text = " (<font color=red><b>" + NewslistHelper.GetCountNews("templist_btv") + "</b></font>)";
            ltrDelList.Text = " (<font color=red><b>" + NewslistHelper.GetCountNews("dellist_btv") + "</b></font>)";
            ltrEditWait.Text = "(<font color=red><b>" + NewslistHelper.GetCountNews("editwaitlist_btv") + "</b></font>)";
            ltrEditedList.Text = "(<font color=red><b>" + NewslistHelper.GetCountNews("editinglist_btv") + "</b></font>)";
            ltrBackList.Text = "(<font color=red><b>" + NewslistHelper.GetCountNews("backlist_btv") + "</b></font>)";
            ltrSendEditWait.Text = "(<font color=red><b>" + NewslistHelper.GetCountNews("sendapprovallist") + "</b></font>)";

            ltrListAlbums.Text = "(<font color=red><b>" + PhotoAlbumHelper.CountPhotoAlbums("album_templist_btv") + "</b></font>)";
            ltrDelListAlbum.Text = "(<font color=red><b>" + PhotoAlbumHelper.CountPhotoAlbums("album_dellist_btv") + "</b></font>)";
            ltrEditWaitAlbum.Text = "(<font color=red><b>" + PhotoAlbumHelper.CountPhotoAlbums("album_editwaitlist_btv") + "</b></font>)";
            ltrEditedListAlbums.Text = "(<font color=red><b>" + PhotoAlbumHelper.CountPhotoAlbums("album_editinglist_btv") + "</b></font>)";
            ltrBackAlbums.Text = "(<font color=red><b>" + PhotoAlbumHelper.CountPhotoAlbums("album_backlist_btv") + "</b></font>)";
            ltrSendEditWaitAlbum.Text = "(<font color=red><b>" + PhotoAlbumHelper.CountPhotoAlbums("album_sendapprovallist") + "</b></font>)";
            */
            #endregion

            
                CreateMenuAlbum();
                CreateMenuNews();
            }
        }

        private void SaveCache(string strCacheName, string value)
        {
            HttpContext.Current.Cache.Add(strCacheName, value, null, DateTime.Now.AddHours(24), TimeSpan.Zero, CacheItemPriority.High, null);
        }

        #region dlMenuNews_ItemDataBound
        protected void dlMenuNews_ItemDataBound(object sender, System.Web.UI.WebControls.DataListItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                Literal ltrCount = (Literal)e.Item.FindControl("ltrCount");
                HiddenField IsCount = (HiddenField)e.Item.FindControl("IsCount");
                HiddenField Cpmode = (HiddenField)e.Item.FindControl("Cpmode");
                HtmlGenericControl divItem = (HtmlGenericControl)e.Item.FindControl("divItem");
                String strCpmode = Request.QueryString["cpmode"];
                //if (Cpmode.Value == strCpmode)
                //if (strCpmode.IndexOf(Cpmode.Value) >= 0)
                //{
                //    divItem.Attributes.Remove("class");
                //    divItem.Attributes.Add("class", "Menuleft_Item_Select");
                //}

                if (Session["cpmode"] != null)
                {
                    String cpmodeSession = Session["cpmode"].ToString();
                    if (cpmodeSession == Cpmode.Value)
                    {
                        divItem.Attributes.Remove("class");
                        divItem.Attributes.Add("class", "Menuleft_Item_Select");
                    }
                }

                if (IsCount.Value.Equals("True"))
                {
                    ltrCount.Text = " (<font color=red><b>" + NewslistHelper.GetCountNews(Cpmode.Value) + "</b></font>)";
                }
            }
        }
        #endregion
        #region dlMenuAlbum_ItemDataBound
        protected void dlMenuAlbum_ItemDataBound(object sender, System.Web.UI.WebControls.DataListItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                Literal ltrCount = (Literal)e.Item.FindControl("ltrCount");
                HiddenField IsCount = (HiddenField)e.Item.FindControl("IsCount");
                HiddenField Cpmode = (HiddenField)e.Item.FindControl("Cpmode");
                HtmlGenericControl divItem = (HtmlGenericControl)e.Item.FindControl("divItem");
                String strCpmode = Request.QueryString["cpmode"];
                //if (Cpmode.Value == strCpmode)
                ////if (strCpmode.IndexOf(Cpmode.Value) >= 0)
                //{
                //    divItem.Attributes.Remove("class");
                //    divItem.Attributes.Add("class", "Menuleft_Item_Select");
                //}
                //else
                if (Session["cpmode"] != null)
                {
                    String cpmodeSession = Session["cpmode"].ToString();
                    if (cpmodeSession == Cpmode.Value)
                    {
                        divItem.Attributes.Remove("class");
                        divItem.Attributes.Add("class", "Menuleft_Item_Select");
                    }
                }

                if (IsCount.Value.Equals("True"))
                {
                    ltrCount.Text = " (<font color=red><b>" + PhotoAlbumHelper.CountPhotoAlbums(Cpmode.Value) + "</b></font>)";
                }
            }
        }
        #endregion

        protected void dlMenuAlbum_ItemCommand(object source, DataListCommandEventArgs e)
        {
            Session["cpmode"] = e.CommandName;
            Response.Redirect(e.CommandArgument.ToString());
        }
    }
}